<template>
  <el-container class="flow-info-container">
    <el-main>
      <!-- 已阅任务 -->
      <div style="position: relative;">
        <el-form ref="formMyTask" :model="formMyTask" label-width="100px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
          <FilterBox :item-width="350">
            <el-form-item label="流程名称" prop="formFilter.processDefinitionName">
              <el-input
                v-model="formMyTask.formFilter.processDefinitionName"
                class="filter-item"
                :clearable="true"
                placeholder="流程名称"
              />
            </el-form-item>
            <el-form-item label="流程标识" prop="formFilter.processDefinitionKey">
              <el-input
                v-model="formMyTask.formFilter.processDefinitionKey"
                class="filter-item"
                :clearable="true"
                placeholder="流程标识"
              />
            </el-form-item>
            <el-form-item label="任务名称" prop="formFilter.taskName">
              <el-input
                v-model="formMyTask.formFilter.taskName"
                class="filter-item"
                :clearable="true"
                placeholder="任务名称"
              />
            </el-form-item>
            <el-button slot="operator" type="default" :plain="true" :size="defaultFormItemSize" @click="onReset">重置</el-button>
            <el-button slot="operator" type="primary" :plain="true" :size="defaultFormItemSize" @click="refreshMyTask(true)">查询</el-button>
          </FilterBox>
        </el-form>
        <el-row>
          <el-col :span="24">
            <el-table
              ref="flowEntry"
              border
              :header-cell-style="{background:'#f4f4f4', color:'#717171' }"
              :data="formMyTask.taskWidget.dataList"
              :size="defaultFormItemSize"
              header-cell-class-name="table-header-gray"
              @sort-change="formMyTask.taskWidget.onSortChange"
            >
              <el-table-column label="序号" header-align="center" align="center" type="index" width="55px" :index="formMyTask.taskWidget.getTableIndex" />
              <el-table-column label="流程名称" prop="processDefinitionName" />
              <el-table-column label="当前任务" prop="taskName" />
              <el-table-column label="任务发起人" prop="processInstanceInitiator" />
              <el-table-column label="任务发起时间" prop="processInstanceStartTime" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button class="table-btn primary" :size="defaultFormItemSize" type="text" @click="onSubmit(scope.row)">办理</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="end" style="margin-top: 10px;">
              <el-pagination
                :total="formMyTask.taskWidget.totalCount"
                :current-page="formMyTask.taskWidget.currentPage"
                :page-size="formMyTask.taskWidget.pageSize"
                :page-sizes="[10, 20, 50, 100]"
                layout="total, prev, pager, next, sizes"
                @current-change="formMyTask.taskWidget.onCurrentPageChange"
                @size-change="formMyTask.taskWidget.onPageSizeChange"
              />
            </el-row>
          </el-col>
        </el-row>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import '@/staticDict/flowStaticDict.js'
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget } from '@/utils/widget.js'
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins'
/* eslint-disable-next-line */
import { FlowDictionaryController, FlowOperationController } from '@/api/flowController.js'
import FilterBox from '@/components/FilterBox'
import '@/components/Dialog'

export default {
  name: 'FormMyTask',
  components: {
    FilterBox
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      formMyTask: {
        formFilter: {
          processDefinitionName: undefined,
          processDefinitionKey: undefined,
          taskName: undefined
        },
        formFilterCopy: {
          processDefinitionName: undefined,
          processDefinitionKey: undefined,
          taskName: undefined
        },
        taskWidget: new TableWidget(this.loadTaskData, this.loadTaskDataVerify, true, false, 'entryId', 1),
        isInit: false
      }
    }
  },
  mounted() {
    // 初始化页面数据
    this.formInit()
  },
  methods: {
    onReset() {
      this.$refs.formMyTask.resetFields()
      this.refreshMyTask(true)
    },
    loadTaskData(params) {
      if (params == null) params = {}
      params = {
        processDefinitionName: this.formMyTask.formFilterCopy.processDefinitionName,
        processDefinitionKey: this.formMyTask.formFilterCopy.processDefinitionKey,
        taskName: this.formMyTask.formFilterCopy.taskName,
        ...params
      }
      return new Promise((resolve, reject) => {
        FlowOperationController.listRuntimeTask(params).then(res => {
          resolve({
            dataList: res.data.dataList,
            totalCount: res.data.totalCount
          })
        }).catch(e => {
          reject(e)
        })
      })
    },
    loadTaskDataVerify() {
      this.formMyTask.formFilterCopy.processDefinitionName = this.formMyTask.formFilter.processDefinitionName
      this.formMyTask.formFilterCopy.processDefinitionKey = this.formMyTask.formFilter.processDefinitionKey
      this.formMyTask.formFilterCopy.taskName = this.formMyTask.formFilter.taskName
      return true
    },
    onSubmit(row) {
      const params = {
        processInstanceId: row.processInstanceId,
        processDefinitionId: row.processDefinitionId,
        taskId: row.taskId
      }

      FlowOperationController.viewRuntimeTaskInfo(params).then(res => {
        if (res.data) {
          this.$router.push({
            name: res.data.routerName || 'handlerFlowTask',
            query: {
              isRuntime: true,
              taskId: row.taskId,
              processDefinitionKey: row.processDefinitionKey,
              processInstanceId: row.processInstanceId,
              processDefinitionId: row.processDefinitionId,
              formId: res.data.formId,
              routerName: res.data.routerName,
              readOnly: res.data.readOnly,
              taskName: row.taskName,
              flowEntryName: row.processDefinitionName,
              processInstanceInitiator: row.processInstanceInitiator,
              // 过滤掉加签操作，加签只有在已完成任务里可以操作
              operationList: (res.data.operationList || []).filter(item => {
                return item.type !== this.SysFlowTaskOperationType.CO_SIGN && item.type !== this.SysFlowTaskOperationType.REVOKE
              }),
              variableList: res.data.variableList
            }
          })
        }
      }).catch(e => {})
    },
    refreshMyTask(reloadData = false) {
      if (reloadData) {
        this.formMyTask.taskWidget.refreshTable(true, 1)
      } else {
        this.formMyTask.taskWidget.refreshTable()
      }
    },
    onResume() {
      this.refreshMyTask()
    },
    formInit() {
      this.refreshMyTask()
    }
  }
}
</script>

<style  lang="scss">
  .flow-info-container {
    display: flex;

    flex-direction: column;
    margin: 0 auto;
    .blueLink {
      color: #409eff;
      font-size: 14px;
      margin-left: 20px;
      cursor: pointer;
    }
    .btnList {
      margin-top:20px;
      display: flex;
      justify-content: right;
      float: right;
      .submitBtn {
        margin-right: 30px;
      }
    }
    .fromFlex {
      height: 36px;
      display: flex;
      align-items: center;
    }
    .base-info {
      display: flex;
      margin-bottom: 20px;
      .basics-info {
        width: 100%;
      }
    }
  }
</style>
